package com.pyg.goods.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.pyg.pojo.TbCitiesExample;
import com.pyg.pojo.TbCitiesExample.Criteria;
import com.pyg.mapper.TbCitiesMapper;
import com.pyg.pojo.TbCities;
import com.pyg.goods.service.TbCitiesService;

import com.pyg.utils.PygStringUtils;
import entity.PageResult;

/**	*  @Author zhuliangcai
*  @Email zhuliangcai@itcast.cn
*  @Date 2018-04-03 20:07:45
*/
@Service
public class TbCitiesServiceImpl implements TbCitiesService {
	@Autowired
	private TbCitiesMapper tbCitiesMapper;

	@Override
	public List<TbCities> findAll() {
		return tbCitiesMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		Page<TbCities> page = (Page<TbCities>) tbCitiesMapper.selectByExample(null);

		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 品牌条件分页
	 *
	 * @param TbCities
	 *            查询条件
	 * @param pageNum
	 *            当前页面
	 * @param pageSize
	 *            每页记录数
	 * @return
	 */
	@Override
	public PageResult findPage(TbCities tbCities, int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		TbCitiesExample example = null;
		if (tbCities != null) {
			example = new TbCitiesExample();
			Criteria criteria = example.createCriteria();
			if (PygStringUtils.isNotEmpty(tbCities.getCityid())) {
				criteria.andCityidLike("%" + tbCities.getCityid() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbCities.getCity())) {
				criteria.andCityLike("%" + tbCities.getCity() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbCities.getProvinceid())) {
				criteria.andProvinceidLike("%" + tbCities.getProvinceid() + "%");
			}
		}

		Page<TbCities> page = (Page<TbCities>) tbCitiesMapper.selectByExample(example);

		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbCities tbCities) {
		tbCitiesMapper.insert(tbCities);
	}

	@Override
	public void del(Integer[] ids) {
		for (Integer id : ids) {
			tbCitiesMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public TbCities findOne(Integer id) {
		return tbCitiesMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(TbCities tbCities) {
		tbCitiesMapper.updateByPrimaryKey(tbCities);
	}

}
